import json
import sys
import time
import io
# 设置sys.stdout的编码为UTF-8
sys.stdout.reconfigure(encoding='utf-8')
# 创建一个新的输出流，并设置编码为UTF-8
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
# 将 当且路径 添加到path中
sys.path.append(r'.')
from DrissionPage import ChromiumPage
from DrissionPage import ChromiumOptions




def load_param_datas_from_file(file_path):
    with open(file_path, "r", encoding='utf-8') as f:
        json_file_datas = json.loads(f.read())  # load的传入参数为字符串类型
        return json_file_datas


def login_qn():
    co = ChromiumOptions()
    #co.add_extension(r'D:\\gitee202406\\my_chrome_extension')
    #co.set_user_data_path(r'D:\\shop-tools\\userdatas\\user9334')


    json_file_datas = load_param_datas_from_file('taobao_login.data.json')
    port = json_file_datas['port']
    username = json_file_datas['username']
    password = json_file_datas['password']

    loginIp = json_file_datas['loginIp']
    loginUrl = json_file_datas['loginUrl']
    nameXpath = json_file_datas['nameXpath']
    passwordXpath = json_file_datas['passwordXpath']
    buttonXpath = json_file_datas['buttonXpath']
    page = ChromiumPage(addr_or_opts=(loginIp) + ':' + str(port))
    # page.get('https://loginmyseller.taobao.com/?from=taobaoindex')
    page.get(loginUrl)
    # 等待页面加载
    page.wait.load_start()

    # 用户名
    # name_input_xpth = '//*[@id="fm-login-id"]'
    name_input = page.ele('xpath:' + nameXpath)
    name_input.click()
    name_input.input(username)
    time.sleep(1)

    # 秘密
    # password_input_xpth = '//*[@id="login-form"]/div[2]/div[2]'
    pwd_input = page.ele('xpath:' + passwordXpath)
    pwd_input.click()
    pwd_input.input(password)
    time.sleep(1)

    # 登录按钮
    # button_xpath = '//*[@id="login-form"]/div[5]/button'
    page.ele('xpath:' + buttonXpath).click()
    time.sleep(2)
    # print(page.url)


login_qn()
